package com.blue.http.controller;

import com.blue.core.dict.HttpMethod;
import com.blue.core.util.StringUtil;
import com.blue.http.annotation.Http;
import com.blue.http.message.Download;
import com.blue.http.message.HttpRequest;
import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.exporter.common.TextFormat;
import io.prometheus.client.hotspot.DefaultExports;
import org.springframework.stereotype.Controller;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author zhengjin
 * @since 1.0 2018年12月30日
 */
@Controller
@Http(url = "/prometheus", method = {HttpMethod.GET}, contentType = "text/plain; charset=utf-8;")
public class PrometheusController
{
	private CollectorRegistry registry;

	public PrometheusController()
	{
		DefaultExports.initialize();
		this.registry = CollectorRegistry.defaultRegistry;
	}

	public String index(HttpRequest request)
	{
		String text = null;
		try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
				PrintWriter pw = new PrintWriter(baos))
		{
			TextFormat.write004(pw, this.registry.metricFamilySamples());
			pw.flush();
			text = baos.toString(StringUtil.UTF8);
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		return text;
	}

}
